package com.hbyj.struts.action;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.hbyj.db.function.CDBInfo;
import com.hbyj.db.function.DbTools;
import com.hbyj.hbm.vo.Jbqk01;
import com.hbyj.hbm.vo.YjdcYjdc01;
import com.opensymphony.xwork2.ActionSupport;


public class YjdcJbqkByPBAction extends ActionSupport{
	
	//对象
	private YjdcYjdc01 yjdc01;
	
	
	
	public YjdcYjdc01 getYjdc01() {
		return yjdc01;
	}



	public void setYjdc01(YjdcYjdc01 yjdc01) {
		this.yjdc01 = yjdc01;
	}



	public void insertYjdcByPb() throws IOException{
		HttpServletRequest request = ServletActionContext.getRequest();
		String yjdc01330 = request.getParameter("yjdc01330");
		String yjdc01140 = request.getParameter("yjdc01140");

		String dBService = request.getParameter("dBService");
		String dBPort = request.getParameter("dBPort");
		String dBName = request.getParameter("dBName");
		String userName = request.getParameter("userName");
		String passWord = request.getParameter("passWord");
		String projectName = request.getParameter("projectName");
		String yjdcdName = request.getParameter("yjdcdName");
		String yjdcdType = request.getParameter("yjdcdType");
		CDBInfo dbinfo = new CDBInfo();
		dbinfo.setdBService(dBService);
		dbinfo.setdBPort(dBPort);
		dbinfo.setUserName(userName);
		dbinfo.setPassWord(passWord);
		dbinfo.setdBName(dBName);
		yjdc01.setYjdc01330(yjdc01330);
		yjdc01.setYjdc01140(yjdc01140);
		//去请求图片
		String ip = request.getRemoteAddr();
		boolean flag1 = true;
		boolean flag2 = true;
		boolean flag3 = true;
		if(!yjdc01.getYjdc01300().equals("")){
			String url = "http://"+ip+":8080/"+projectName+"/uploadfolder/simple/yjdc/image/"+yjdc01.getYjdc01300();
			flag1 = getImgFileByPB(url,yjdc01.getYjdc01300());
		}
		if(!yjdc01.getYjdc01310().equals("")){
			String url = "http://"+ip+":8080/"+projectName+"/uploadfolder/simple/yjdc/image/"+yjdc01.getYjdc01310();
			flag2 = getImgFileByPB(url,yjdc01.getYjdc01310());
		}
		if(!yjdc01.getYjdc01320().equals("")){
			String url = "http://"+ip+":8080/"+projectName+"/uploadfolder/simple/yjdc/image/"+yjdc01.getYjdc01320();
			flag3 = getImgFileByPB(url,yjdc01.getYjdc01320());
		}
		Object jsoncallback = request.getParameter("jsoncallback");
		yjdc01.setId(null);
		String time =String.valueOf(yjdc01.getYjdc01270().getTime()); 
		String jbqk0102 = yjdc01.getJbqk0102();
		//添加jbqk0102这个字段的功能处理By20130505
		boolean flag4 = findJbqkByYjdcd(yjdc01.getYjdc01000(),yjdcdName,yjdcdType);
		
		if(flag1&&flag2&&flag3&&flag4&&DbTools.saveObj(yjdc01)){
			if(addSketchByYjdcPb(dbinfo,yjdc01.getYjdc01000(),time,jbqk0102)&&addDwxxByYjdcPb(dbinfo,yjdc01.getYjdc01000(),time,jbqk0102)){////拷贝素描图从手持到服务器端
				String str = jsoncallback+"('true')";
				ServletActionContext.getResponse().getOutputStream().write(str.getBytes("utf-8"));
			}else{
				String str = jsoncallback+"('false')";
				ServletActionContext.getResponse().getOutputStream().write(str.getBytes("utf-8"));
			}
		}else{
			String str = jsoncallback+"('false')";
			ServletActionContext.getResponse().getOutputStream().write(str.getBytes("utf-8"));
		}
		
	}
	
	private boolean findJbqkByYjdcd(String tybh,String yjdcdName,String yjdcdType){
		boolean flag = false;
		String hql = "from Jbqk01 where id.jbqk0100 = '"+tybh +"' and jbqk0110='"+yjdcdName+"' and id.jbqk0102 = '3'";
		List list = DbTools.findByHql(hql);
		if(list.size()>0){
			flag = true;
		}else{
			String hql1 = "from Jbqk01 where id.jbqk0100 = '"+tybh+"' and id.jbqk0102='"+yjdcdType+"'";
			List<Jbqk01> list1 = DbTools.findByHql(hql1);
			if(list1.size()>0){
				Jbqk01 jbqk01 = list1.get(0);
				jbqk01.getId().setJbqk0102("3");
				if(DbTools.saveObj(jbqk01)){
					flag = true;
				}
			}
		}
		return flag;
	}
	
	private boolean addSketchByYjdcPb(CDBInfo dbinfo,String tybh,String time,String jbqk0102){
		return DbTools.getSketchCopyRes(dbinfo, "Sketch", tybh, time,jbqk0102);
	}
	private boolean addDwxxByYjdcPb(CDBInfo dbinfo,String tybh,String time,String jbqk0102){
		return DbTools.addDwxxByYjdcPb(dbinfo, tybh, time,jbqk0102);
	}
	private boolean  getImgFileByPB(String url,String imgName) throws IOException{
		URL reqUrl = new URL(url);
		URLConnection urlCon = reqUrl.openConnection();
		String serverPath="uploadfolder/simple/yjdc/image";
		String getFilePath = ServletActionContext.getServletContext().getRealPath("/"+serverPath);
        String  newFileName  = imgName;
        File file = new File(getFilePath +"/"+newFileName);
        boolean flag = saveFile(urlCon,file);
        return flag;
	}
	
	
	private boolean saveFile(URLConnection connection,File outFile)
  	{
  		try {
	  		InputStream fin = connection.getInputStream();
			FileOutputStream fout = new FileOutputStream(outFile);
			byte[] buf=new byte[1024]; 
			int num = 0;	
			while ((num=fin.read(buf)) != -1) 
			{ 
				fout.write(buf,0,num);
			}
			fout.close();
			fin.close();
			return true;
  		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
  	}
	
}